package com.app.web.base.controller;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.app.web.base.model.User;
import com.app.web.base.service.UserService;
import com.app.web.base.util.DateUtil;
import com.app.web.base.util.controller.PageDateModel;


/**
 * Project: erp<br>
 * Description: <br>
 * Copyright (c) 2012<br>
 * All Rights Reserved.<br>
 * @author <a href="mailto:gly420@163.com">GuanLuYong</a><br>
 */
@Controller @Scope("view")
public class UserBean extends BaseBean<User> implements Serializable {
	private static final long serialVersionUID = 1L;
	@Resource(name = "userService")
	private UserService userService;
	
	// 新增实体初始化
	public void initAddUser() {
		instance = new User();
		instance.setStatus(1l);
	}
	
	@Override
	public void search() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
	    String prefix = "SELECT u ";
		String ttPrefix = "SELECT COUNT(u) ";
		StringBuilder suffix = new StringBuilder("FROM User u WHERE 1=1 ");
		Object code = queryMap.get("code");
		if(code != null && !"".equals(code.toString())) {
			suffix.append("AND u.code LIKE '" + code.toString() + "%' ");
		}
		Object name = queryMap.get("name");
		if(name != null && !"".equals(name.toString())) {
			suffix.append("AND u.name LIKE '%" + name.toString() + "%' ");
		}
		Object sex = queryMap.get("sex");
		if(sex != null && !"".equals(sex.toString())) {
			suffix.append("AND u.sex = '" + sex + "' ");
		}
		Object startBirth = queryMap.get("startBirth");
		if(startBirth != null) {
			suffix.append("AND u.birth >= '" + ((Date) startBirth).toLocaleString() + "' ");
		}
		Object endBirth = queryMap.get("endBirth");
		if(endBirth != null) {
			suffix.append("AND u.addDate < '" + DateUtil.getAfterOneDay((Date) endBirth).toLocaleString() + "' ");
		}
		String orderby = "ORDER BY u.code";
		lazyData = new PageDateModel<User>(userService, prefix, ttPrefix, suffix.toString(), orderby, null);
	}
	@Override
	public void editEntity() throws Exception {
		super.editEntity();
		userService.update(instance);
	}
	
	@Override
	public void addEntity() throws Exception {
		super.addEntity();
		userService.insert(instance);
	}
	
	@Override
	public void removeEntity() throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	// Getter & Setter
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

}
